#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "bezier.h"
#include <QGraphicsScene>
#include <QGraphicsView>
#include "QFileDialog"
#include <QGraphicsTextItem>
#include <QPointF>
#include <QGraphicsPixmapItem>
#include <QGraphicsItemAnimation>
#include <QTimeLine>
#include <QSpinBox>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    //setando plano de fundo no graphicsView
    repaintBackground();
    QPointF p1(ui->boxP1x->value(),ui->boxP1y->value());
    QPointF p2(ui->boxP2x->value(),ui->boxP2y->value());
    QPointF p3(ui->boxP3x->value(),ui->boxP3y->value());
    QPointF p4(ui->boxP4x->value(),ui->boxP4y->value());

    curva = new Bezier(this);
    curva->setPoints(p1, p2, p3, p4);
    curva->desenhaPontos(scene, p1,p2,p3,p4);
    curva->desenhaCurva(scene);
    ui->graphicsView->setScene(scene);

    tempo = new QTimeLine(55000);
    verificaMomento = 0;

}


MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::repaintBackground(){
    scene = new QGraphicsScene;
    QImage source("ImagemFundo.jpg");
    QPixmap img = QPixmap::fromImage(source);
    scene->addPixmap(img);
}

void MainWindow::on_buttonAddSprite_clicked()
{
    //repintando o fundo + a curva
    repaintBackground();

    //pegando o arquivo para o Sprite
    QString src2 = QFileDialog::getOpenFileName();
    QImage source2(src2);
    QPixmap img2 = QPixmap::fromImage(source2);
    ui->labelSprite->setPixmap(img2);

    sprite = new QGraphicsPixmapItem(img2);

}

void MainWindow::on_boxP1x_valueChanged(int arg1)
{
    repaintBackground();
    curva->setP1(QPointF((qreal)arg1, curva->getP1().y()));
    curva->desenhaPontos(scene, curva->getP1(), curva->getP2(), curva->getP3(), curva->getP4());
    curva->desenhaCurva(scene);
    ui->graphicsView->setScene(scene);
}

void MainWindow::on_boxP1y_valueChanged(int arg1)
{
    repaintBackground();
    curva->setP1(QPointF(curva->getP1().x(), (qreal)arg1));
    curva->desenhaPontos(scene, curva->getP1(), curva->getP2(), curva->getP3(), curva->getP4());
    curva->desenhaCurva(scene);
    ui->graphicsView->setScene(scene);
}

void MainWindow::on_boxP2x_valueChanged(int arg1)
{
    repaintBackground();
    curva->setP2(QPointF((qreal)arg1, curva->getP2().y()));
    curva->desenhaPontos(scene, curva->getP1(), curva->getP2(), curva->getP3(), curva->getP4());
    curva->desenhaCurva(scene);
    ui->graphicsView->setScene(scene);

}

void MainWindow::on_boxP2y_valueChanged(int arg1)
{
    repaintBackground();
    curva->setP2(QPointF(curva->getP2().x(), (qreal)arg1));
    curva->desenhaPontos(scene, curva->getP1(), curva->getP2(), curva->getP3(), curva->getP4());
    curva->desenhaCurva(scene);
    ui->graphicsView->setScene(scene);

}

void MainWindow::on_boxP3x_valueChanged(int arg1)
{
    repaintBackground();
    curva->setP3(QPointF((qreal)arg1, curva->getP3().y()));
    curva->desenhaPontos(scene, curva->getP1(), curva->getP2(), curva->getP3(), curva->getP4());
    curva->desenhaCurva(scene);
    ui->graphicsView->setScene(scene);

}

void MainWindow::on_boxP3y_valueChanged(int arg1)
{
    repaintBackground();
    curva->setP3(QPointF(curva->getP3().x(), (qreal)arg1));
    curva->desenhaPontos(scene, curva->getP1(), curva->getP2(), curva->getP3(), curva->getP4());
    curva->desenhaCurva(scene);
    ui->graphicsView->setScene(scene);
}

void MainWindow::on_boxP4x_valueChanged(int arg1)
{
    repaintBackground();
    curva->setP4(QPointF((qreal)arg1, curva->getP4().y()));
    curva->desenhaPontos(scene, curva->getP1(), curva->getP2(), curva->getP3(), curva->getP4());
    curva->desenhaCurva(scene);
    ui->graphicsView->setScene(scene);
}

void MainWindow::on_boxP4y_valueChanged(int arg1)
{
    repaintBackground();
    curva->setP4(QPointF(curva->getP4().x(), (qreal)arg1));
    curva->desenhaPontos(scene, curva->getP1(), curva->getP2(), curva->getP3(), curva->getP4());
    curva->desenhaCurva(scene);
    ui->graphicsView->setScene(scene);
}

void MainWindow::on_buttonPlay_clicked()
{
    if(verificaMomento == 0){
           ui->buttonPlay->setText("Pause");

           //sprite->setPos(ui->boxP1x->value(),ui->boxP1y->value());

            //QTimeLine *tempo = new QTimeLine(55000+(ui->boxVelocidade->value()*-5500));
           tempo->setDuration(55000+(ui->boxVelocidade->value()*-5500));
           tempo->setFrameRange(0, 100);

           animacao = new QGraphicsItemAnimation(this);
           animacao->setItem(sprite);
           animacao->setTimeLine(tempo);

           curva->percorrerCurva(animacao);

           ui->graphicsView->scene()->addItem(animacao->item());
           tempo->start();
           verificaMomento = 1;
       }
       else if(verificaMomento == 1){
           ui->buttonPlay->setText("Play");
           tempo->setPaused(true);
           verificaMomento = 2;
       }
       else{
           ui->buttonPlay->setText("Pause");
           tempo->resume();
           verificaMomento = 1;
       }
}

void MainWindow::on_buttonStop_clicked()
{
    tempo->stop();
        ui->graphicsView->scene()->removeItem(animacao->item());
        tempo->setCurrentTime(0);
        verificaMomento = 0;
        ui->buttonPlay->setText("Play");
}

void MainWindow::on_RadioIda_toggled(bool checked)
{
    curva->setSoIda(checked);
}
